define(function (require, exports, module) {
        exports.init = function (element, cb) {
                var basic=require('basic');
                var jsonAPI = require('json-api');
                var mustache = require('mustache');

                $("#title").text('我的订单');
                defineModule({
                        listOrderRecord:function(cb)
                        {
                                var mobile=basic.getLoginUser();
                                jsonAPI.restful('/api/orderRecord/list', {mobile:mobile}, function (err, result) {
                                        if (jsonAPI.handleErrors(err, result)) {

                                         /*       var tpl = $('#tpl_order').html();
                                                var content = mustache.render(tpl, mustache.normalizeInput(result));
                                                $('#orderList').html(content);*/
                                                var data=result.result;
                                               for (var i = 0; i < data.length; i++) {
                                                        var status, d = data[i], action, entertime = new Date(d.entertime.replace(/-/g, "/")), lefttime = new Date(), discount = 0, paymentmethod = "", haspaid = parseInt(d.haspaid), cashpaymentfee = (d.cashpaymentfee?parseInt(d.cashpaymentfee):0);
                                                        if (d.lefttime)
                                                                lefttime = new Date(d.lefttime.replace(/-/g, "/"));
                                                        if (d.discount)
                                                                discount = d.discount;

                                                        paymentmethod = '<span class="fr">';
                                                        //if (d.parkingfee > 0) {
                                                        switch (d.paymentmethod) {
                                                                case "alipay":
                                                                        paymentmethod += '支付宝支付' + ((d.parkingfee - discount) / 100) + '元';
                                                                        break;
                                                                case "wx":
                                                                        paymentmethod += '微信支付' + ((d.parkingfee - discount) / 100) + '元'; console.log(paymentmethod)
                                                                        break;
                                                                case "wx_pub":
                                                                        paymentmethod += '微信支付' + ((d.parkingfee - discount) / 100) + '元';
                                                                        break;
                                                        }
                                                        // }
                                                        if (d.cashpaymentfee) {
                                                                if (d.paymentmethod){
                                                                        paymentmethod += "，现金支付" + d.cashpaymentfee / 100 + "元";
                                                                }
                                                                else
                                                                        paymentmethod += "现金支付" + d.cashpaymentfee / 100 + "元";
                                                        }
                                                        paymentmethod += '</span>';

                                                        //alert(entertime +" "  + d.lefttime +" "  + haspaid +" "  + cashpaymentfee)
                                                        //停靠中- 有entertime, 没有lefttime
                                                        if (entertime && !d.lefttime && haspaid == 0) {
                                                                status = "停靠中"; action = '<a  style="margin-bottom: 10px" class="btn2 bgc5 fr w30 tc ft15" href="#orderDetail?id=' + d.id + '">查看</a>';
                                                        }
                                                        //驶离中- 有entertime，没有lefttime，haspaid==1
                                                        else if (entertime && !d.lefttime && haspaid == 1) {
                                                                status = "驶离中"; action = '<a  style="margin-bottom: 10px" class="btn2 bgc5 fr w30 tc ft15" href="#orderLeft?id=' + d.id + '">查看</a>';
                                                        }
                                                        //已支付- 有entertime, 有lefttime, haspaid==1 或 cashpaymentfee>0
                                                        else if (entertime && d.lefttime && (haspaid == 1 || cashpaymentfee>0)) {
                                                                status = d.parkingfee / 100 + "元"; action = '<span class="fr">' + paymentmethod + '</span>';
                                                        }
                                                        //已离场未支付 - 有entertime, 有lefttime, haspaid==0 或 cashpaymentfee==0
                                                        else if (entertime && d.lefttime && haspaid == 0 && !cashpaymentfee) {
                                                                status = "已离场未支付"; action = '<a class="btn2 bgc3 fr w30 tc ft15" href="#payCoupon.html?id=' + d.id + '">支付</a>'
                                                        }

                                                        var date3 = lefttime.getTime() - entertime.getTime(), hours = (date3 / (3600 * 1000)).toFixed(2);

                                                        $("#orderList").append('<li><div class="w90 panel pt10"><span class="ft15">' + d.parkingname + '</span>（' + d.licenseplate + '）<span class="fr c3">' + status + '</span><p>停车' + hours + '小时（' + entertime.format("HH:mm") + '-' + (d.lefttime ? lefttime.format("HH:mm") : '') + '）</p><p class="cf mt15"><span class="fl c3">' + entertime.format("yyyy-MM-dd") + '</span>' + action + '</p></div></li>');
                                                }
                                        }
                                        cb && cb();
                                });
                        }
                })

                $module.listOrderRecord(function(){
                        cb && cb();
                })
        };
});